package com.meichis.yndms.adapter.RecycleViewItemType;

import android.app.Activity;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.meichis.mcsappframework.adapter.recyclerview.base.ItemViewDelegate;
import com.meichis.mcsappframework.adapter.recyclerview.base.ViewHolder;
import com.meichis.yndms.R;
import com.meichis.yndms.model.InspectItemOption;
import com.meichis.yndms.model.InspectTemplate_Item;
import com.meichis.yndms.ui.activity.VST_FreeQuestionnaireLogicActivity;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class FillMode_CheckBox implements ItemViewDelegate<InspectTemplate_Item> {
    private final Activity activity;
    private List<InspectTemplate_Item> items;

    public FillMode_CheckBox(Activity activity, List<InspectTemplate_Item> items) {
        this.items = items;
        this.activity = activity;
    }

    @Override
    public int getItemViewLayoutId() {
        return R.layout.fill_mode_check_box;
    }

    @Override
    public boolean isForViewType(InspectTemplate_Item item, int position) {
        return item.getItem().getFillMode() == 4;
    }

    @Override
    public void convert(ViewHolder holder, final InspectTemplate_Item item, final int position) {
        TextView tv_group = holder.getView(R.id.tv_group);
        if (position == 0) {
            if (!TextUtils.isEmpty(item.getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        } else {
            if (!TextUtils.isEmpty(item.getItemGroup()) && !item.getItemGroup().equals(items.get(position - 1).getItemGroup())) {
                tv_group.setVisibility(View.VISIBLE);
                holder.setVisible(R.id.v_group, true);
                tv_group.setText(item.getItemGroup());
            } else {
                tv_group.setVisibility(View.GONE);
                holder.setVisible(R.id.v_group, false);
            }
        }

        holder.setText(R.id.tv_question, Html.fromHtml(String.valueOf(position + 1) + ". " +
                item.getItem().getName() +
                (!TextUtils.isEmpty(item.getItem().getUnit()) ? "(" + item.getItem().getUnit() + ")" : "") +
                (item.getIsMustFill() == 1 ? " <font  color=\"#FF0000\">*</font>" : "") +
                " <font  color=\"#6699FF\">[ 多选 ]</font>" +
                (!TextUtils.isEmpty(item.getItem().getSubTitle()) ? "<br/> <font color=\"#8D8D8D\" ><small>" + item.getItem().getSubTitle() + "</small></font>" : "")
        ));
        final LinearLayout ll = holder.getView(R.id.ll_answer);
        setBackground(item, ll);
        ll.removeAllViews();
        int count = item.getItem().getOptions().size();
        for (int i = 0; i < count; i++) {
            CheckBox checkBox = new CheckBox(holder.getConvertView().getContext());
            checkBox.setButtonDrawable(holder.getConvertView().getContext().getResources().getDrawable(R.drawable.checkbox_select));
            checkBox.setPadding(30, 20, 20, 20);
            checkBox.setTextColor(Color.BLACK);
            checkBox.setTextSize(16);
            checkBox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Set<String> fillValues = new HashSet<>(Arrays.asList(item.getFillValue().split("\\|")));
                    if (((CheckBox) v).isChecked()) {
                        fillValues.add(v.getTag().toString());
                    } else {
                        fillValues.remove(v.getTag().toString());
                    }
                    //点击之后设置 "" 不加载默认值
                    item.setFillName("");
                    item.setFillValue("");
                    for (String s : fillValues) {
                        if(item.getFillValue().equals(""))
                        {
                            item.setFillValue(s);
                        }else{
                            item.setFillValue(item.getFillValue() + "|" + s);
                        }
                        for (InspectItemOption option : item.getItem().getOptions()) {
                            if (s.equals(option.getCode())) {
                                if(item.getFillName().equals(""))
                                {
                                    item.setFillName(option.getName());
                                }else{
                                    item.setFillName(item.getFillName() + "|" + option.getName());
                                }
                                break;
                            }
                        }
                    }
                    item.setFillValue(item.getFillValue().substring(item.getFillValue().startsWith("|") ? 1 : 0));
                    item.setFillName(item.getFillName().substring(item.getFillName().startsWith("|") ? 1 : 0));
                    setBackground(item, ll);
                    ((VST_FreeQuestionnaireLogicActivity)activity).notifyAdapter();

                }
            });
            checkBox.setText(item.getItem().getOptions().get(i).getName());
            checkBox.setTag(item.getItem().getOptions().get(i).getCode());
            //包含这个Value 设置Checked
            checkBox.setChecked(Arrays.asList(item.getFillValue().split("\\|")).contains(item.getItem().getOptions().get(i).getCode()));
            ll.addView(checkBox, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        }
    }

    private void setBackground(InspectTemplate_Item item, LinearLayout ll) {
        ll.setBackgroundResource(item.getFillName().length() == 0 && item.getIsMustFill() == 1 ? R.drawable.bg_ll_frame : R.drawable.bg_ll_frame);
    }
}
